package com.example.demoa;



import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;


class StuAdapter extends RecyclerView.Adapter<StuViewHolder> {
    private Context context;
    private ArrayList<Student> studentArrayList;
    private IOnDelListener listener;
    private int i ;

    public StuAdapter(Context context, ArrayList<Student> studentArrayList,IOnDelListener listener) {
        this.context = context;
        this.studentArrayList = studentArrayList;
        this.listener=listener;
    }


    @NonNull
    @Override
    public int getItemCount() {
        return studentArrayList.size();
    }

    @Override
   public StuViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_main,parent,false);

        StuViewHolder viewHolder = new StuViewHolder(view);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(StuViewHolder holder, final int i){
        final Student s = studentArrayList.get(i);

        holder.headView.setImageResource(s.getImgId());
        holder.nameView.setText(s.getName());
        holder.ageView.setText(String.valueOf(s.getAge()));


        holder.view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,s.getName() + "  "+s.getAge(),
                        Toast.LENGTH_SHORT).show();
            }
        });

        holder.ivDel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(i);
                notifyDataSetChanged();
            }
        });

    }


}